package toy.keli.picmap.tools;

import android.Manifest;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

import toy.keli.picmap.R;
import toy.keli.picmap.model.path;
import toy.keli.picmap.model.pic;

public class Progress extends Tools{
    SeekBar mProgresBar;
    TextView mProgresText;

    public SeekBar getmProgresBar() {
        return mProgresBar;
    }

    public TextView getmProgresText() {
        return mProgresText;
    }

    public void progresUpdate(int i, int n, String t){
        if(mProgresBar==null) {
            findViewById(R.id.progressbar).setVisibility(View.VISIBLE);
            findViewById(R.id.progressbar1).setVisibility(View.GONE);
            mProgresText = (TextView) findViewById(R.id.index);
            mProgresBar = (SeekBar) findViewById(R.id.seekBar1);

            View shareBox =  findViewById(R.id.share_box);
            // 年和月不要分享
            findViewById(R.id.share).setVisibility(Control.date[2]<3 ? View.VISIBLE: View.GONE);
            shareBox.setVisibility(View.VISIBLE);
            mProgresBar.setVisibility(View.VISIBLE);
            mProgresBar.setMax(n);
            mProgresBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    if(fromUser) Show.timeAnimationTo(progress);
                }
                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {}

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {}
            });
        }
        if(i==-1){
            mProgresBar.setProgress(0);
            if(n==0) {
                findViewById(R.id.progressbar).setVisibility(View.GONE);
                findViewById(R.id.share_box).setVisibility(View.GONE);
                mProgresBar = null;
            }
            return;
        }
        if(n>0) mProgresBar.setMax(n);
        mProgresBar.setProgress(i);
        mProgresText.setText(t);
    }



    //尝试自动更新图片
    public void tryAutoUpdatePic() {
        //开关
        if(!Control.autoSearchEnable) return;
        //权限
        if(!getPermission(Manifest.permission.READ_EXTERNAL_STORAGE,()->{ tryAutoUpdatePic(); })) {
            msg("请先授权媒体读取权限");
            return;
        }
        //开始拉0000000
        String lastUptime = new path().find().order("uptime desc").getField("uptime");
        if(lastUptime==null) lastUptime="0";
        View p0=findViewById(R.id.progressbar);
        TextView p1=(TextView)findViewById(R.id.index);
        android.widget.ProgressBar p=(android.widget.ProgressBar)findViewById(R.id.progressbar1);
        final boolean[] isFirst = {true};
        pic.findByResolver(act,Long.valueOf(lastUptime),(i, n)->{
            if(n>0){
                if(isFirst[0]) {
                    p0.setVisibility(View.VISIBLE);
                    p.setMax(n);
                    pic picOne = (pic)new pic().findOne();
                    Show.moveTo(picOne.x,picOne.y,12);
                    isFirst[0] = false;
                }
                if(i==n){
                    try {
                        p0.setVisibility(View.GONE);
                        msg("更新图片"+n+"张");
                    }catch (Exception e){}
                }
            }
            p.setProgress(i);
            p1.setText(i+"/"+n);

            if(i%10==0) act.addMarkers();
        });
    }



}
